package cn.supermarket.service.impl;


import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.StockTimeMapper;
import cn.supermarket.pojo.dto.StockTimeAddDTO;
import cn.supermarket.pojo.dto.StockUpdateDTO;
import cn.supermarket.pojo.entity.Product;
import cn.supermarket.pojo.entity.StockTime;
import cn.supermarket.pojo.vo.StockDetailedVO;
import cn.supermarket.service.iservice.IProductService;
import cn.supermarket.service.iservice.IStockService;
import cn.supermarket.utils.GetInfoUtils;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.State;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 实时库存表--- 服务实现类
 * </p>
 *
 * @author the shy
 * @since 2022-07-02
 */
@Service
public class StockServiceImpl implements IStockService {

    @Autowired
    private StockTimeMapper stockTimeMapper;

    @Autowired
    private IProductService productService;


    /**
     * 减少库存
     * @param productId
     * @param counts
     */
    @Override
    public void reduceProductCount(Long productId,BigDecimal counts) {
        BigDecimal oldCounts = stockTimeMapper.selectCountsByProductId(productId);
            //                  比较数值大小，原数值小于修改数值
        if (oldCounts==null || oldCounts.compareTo(counts)==-1) {
            throw new ServiceException(State.ERR_BAD_REQUEST,"库存数量不足，请练习相关工作人员进货");
        }
        BigDecimal newCounts = oldCounts.multiply(counts);
        LocalDateTime now = LocalDateTime.now();
        StockUpdateDTO stockUpdateDTO = new StockUpdateDTO(productId,newCounts,now);
        stockTimeMapper.reduceProductCount(stockUpdateDTO);
    }

    /**
     * 删除库存条目
     * @param productId
     */
    @Override
    public void deleteById(Long productId) {
        if(stockTimeMapper.isExist(productId)!=1){
            throw new ServiceException(State.ERR_NOT_FOUND,"库存中无此条目");
        }
        stockTimeMapper.deleteByProductId(productId);
    }

    /**
     * 按照商品id查询
     * @param productId
     * @return
     */
    @Override
    public StockDetailedVO selectStockDetailedVOByProductId(Long productId) {
        StockDetailedVO stockDetailedVO = stockTimeMapper.selectDetailedByProductId(productId);
        if (stockDetailedVO == null) {
            throw new ServiceException(State.ERR_NOT_FOUND,"库存中无此条目");
        }
        return stockDetailedVO;
    }

    @Override
    public List<StockDetailedVO> selectAll() {
        List<StockDetailedVO> stockDetailedVOS = stockTimeMapper.selectAll();
        if (stockDetailedVOS == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "未查询到相关内容");
        }
        return stockDetailedVOS;
    }

    @Override
    public List<StockDetailedVO> selectByCategoryId(Long categoryId) {

        List<StockDetailedVO> stockDetailedVOS = stockTimeMapper.selectByCategoryId(categoryId);
        if (stockDetailedVOS == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "未查询到相关内容");
        }
        return stockDetailedVOS;
    }

    @Override
    public List<StockDetailedVO> selectByWarningLine() {
        List<StockDetailedVO> stockDetailedVOS = stockTimeMapper.selectByWarningLine();
        if (stockDetailedVOS == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "未查询到相关内容");
        }
        return stockDetailedVOS;
    }

    /**
     * 添加库存条目
     * @param stockTimeAddDTO
     */
    @Override
    public void addNew(StockTimeAddDTO stockTimeAddDTO) {
       Long isExist= stockTimeMapper.isExist(stockTimeAddDTO.getProductId());
       if (isExist == 1) {
           throw new ServiceException(State.ERR_INSERT,"库存中已有该商品，请直接修改库存数量");
       }
        StockTime stockTime = new StockTime();
       BeanUtils.copyProperties(stockTimeAddDTO, stockTime);
       Product product = productService.selectById(stockTimeAddDTO.getProductId());
        SnowFlakeGenerateIdWorker s = new SnowFlakeGenerateIdWorker(16,18);
        LocalDateTime now = LocalDateTime.now();
        stockTime.setUnit(product.getUnit());
        stockTime.setId(s.nextId());
        stockTime.setCreateTime(now);
        stockTime.setLastUpdateTime(now);
        int row = stockTimeMapper.addNew(stockTime);
        if (row !=1) {
            throw new ServiceException(State.ERR_UNKNOWN_ERROR,"未知错误导致信息调用失败，请稍后再试");
        }
    }

    /**
     * 增加库存
     * @param productId
     * @param counts
     */
    @Override
    public void addProductCount(Long productId,BigDecimal counts) {
        BigDecimal oldCounts = stockTimeMapper.selectCountsByProductId(productId);
        //                  比较数值大小，原数值小于修改数值
        if (oldCounts==null ) {
            throw new ServiceException(State.ERR_NOT_FOUND,"库存中无此条目");
        }
        BigDecimal newCounts = oldCounts.add(counts);
        LocalDateTime now = LocalDateTime.now();
        StockUpdateDTO stockUpdateDTO = new StockUpdateDTO(productId,newCounts,now);
        stockTimeMapper.addProductCount(stockUpdateDTO);
    }

}
